const { no, ok } = require('../../utils/utlis');

const articleService = require('../../services/admin/articleService');
module.exports = {
	list: async (req, res, next) => {
		try {
			let { title, type, isHot, isRecommend, pageNum, pageSize } = req.body;
			if (!type) type = 0;
			if (!isHot) isHot = 0;
			if (!isRecommend) isRecommend = 0;

			let data = await articleService.list(title, type, isHot, isRecommend, pageNum, pageSize);
			res.send(ok(data));
		} catch (error) {
			res.send(no(error.message));
		}
	},
	add: async (req, res, next) => {
		try {
			let { title, editor, content, url,filename, isHot, isTop, isRecommend, state, type, abstract, label } = req.body;
			await articleService.add(title, editor, content, url,filename, isHot, isTop, isRecommend, state, type, abstract, label);
			res.send(ok());
		} catch (error) {
			res.send(no(error.message));
		}
	},
	edit: async (req, res, next) => {
		try {
			let { title, editor, content, url,filename, isHot, isTop, isRecommend, state, type, abstract, label, id } = req.body;

			await articleService.edit(title, editor, content, url,filename, isHot, isTop, isRecommend, state, type, abstract, label, id);
			res.send(ok());
		} catch (error) {
			res.send(no(error.message));
		}
	},
	del: async (req, res, next) => {
		try {
			let { id } = req.body;
			await articleService.del(id);
			res.send(ok());
		} catch (error) {
			res.send(no(error.message));
		}
	},
	detail: async (req, res, next) => {
		try {
			let { id } = req.body;
			let obj = await articleService.detail(id);
			res.send(ok(obj));
		} catch (error) {
			res.send(no(error.message));
		}
	}
};
